// services/http.js
import axios from 'axios';

const http = axios.create({
    baseURL: '/api',
    headers: {
        'Content-Type': 'application/json'
    }
});

http.interceptors.request.use(config => {
    // 检查当前请求是否是登录请求
    if (!config.url.includes('/login')) {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
    }
    return config;
}, error => {
    return Promise.reject(error);
});

http.interceptors.response.use(response => {
    // 处理统一的响应
    return response.data;
}, error => {
    // 统一处理错误
    return Promise.reject(error.response.data.message);
});

export default http;
